# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/6/18

library(optparse)
library(tidyverse)
library(magrittr)
library(ropls)

option_list <- list(
make_option("--i", default = "AllMet.csv", type = "character", help = "metabolite data file"),
make_option("--g", default = "SampleInfo.csv", type = "character", help = "sample group file"),
make_option("--sc", default = "sample_color.txt", type = "character", help = "sample color file")
)
opt <- parse_args(OptionParser(option_list = option_list))

sampleInfo <- read.csv(opt$g, header = T, stringsAsFactors = F) %>%
    select(c("SampleID", "ClassNote")) %>%
    as.data.frame()

sampleColDf <- read_csv(opt$sc) %>%
select(c("ClassNote", "col"))  %>%
    filter(ClassNote=="QC") %>%
    add_row(ClassNote="Sample",col="black")
sampleCols <- sampleColDf %>%
deframe()
sampleCols

data <- read_csv(opt$i) %>%
    select(- c("HMDB", "KEGG", "Class")) %>%
    gather("SampleID", "Value", - Metabolite) %>%
    mutate(SampleID=factor(SampleID,levels=unique(SampleID))) %>%
    spread(Metabolite, "Value")
data
x <- data %>%
column_to_rownames("SampleID")

head(x)
crossValI <- min(nrow(x), 7)
pcaRs <- opls(x, plotL = F, crossvalI = crossValI)

pcData <- pcaRs@scoreMN %>%
    as.data.frame() %>%
    rownames_to_column("SampleID") %>%
    select("SampleID", num_range("p", 1 : 5)) %>%
    rename_at(vars(- c("SampleID")), function(x){
        str_replace(x, "p", "PC")
    })

pcData

pc1 <- pcData$PC1
sd <- sd(pc1)
neg3Sd <- (- 3 * sd)
neg2Sd <- (- 2 * sd)
pos3Sd <- (3 * sd)
pos2Sd <- (2 * sd)
sd2Color <- "#FFA500"
sd3Color <- "red"
sdFontSize <- 1.9
pdf("PC1_Dotplot.pdf", width = 8, height = 3)
xend <- nrow(pcData)
pc1 <-pcData %>%
    select(c("SampleID","PC1"))
pc1$id = c(1 : xend)
ids <- pc1$id
names <- pc1$SampleID
textPos <- 0.8
pc1

plotData<-pc1 %>%
    left_join(sampleInfo,by="SampleID") %>%
    mutate_at(vars("ClassNote"),~ifelse(.x!="QC","Sample","QC"))
plotData

p <- ggplot(plotData, mapping = aes(x = id, y = PC1)) +
    xlab("") +
    ylab("PC1 scores/Outlier Detection") +
    theme_bw(base_size = 8.8, base_family = "Times") +
    theme(axis.text.x = element_text(angle = 90, size = 6, hjust = 1, vjust = 0.5),
    axis.text.y = element_text(size = 8.8), legend.position = 'top', panel.grid.minor = element_blank(),
    axis.title.y = element_text(size = 11), legend.margin = margin(t = 0.3, b = - 0.3, unit = 'cm'),
    legend.text = element_text(size = 8)
    ) +
#-3sd
    geom_segment(aes(x = 1, xend = xend, y = neg3Sd, yend = neg3Sd), colour = "red", linetype = "dashed") +
    annotate("text", x = textPos , y = neg3Sd, label = "-3SD", color = sd3Color, hjust = 1, size = sdFontSize) +
#-2sd
    geom_segment(aes(x = 1, xend = xend, y = neg2Sd, yend = neg2Sd), colour = "#FFA500", linetype = "dashed") +
    annotate("text", x = textPos , y = neg2Sd, label = "-2SD", color = sd2Color, hjust = 1, size = sdFontSize) +
#2sd
    geom_segment(aes(x = 1, xend = xend, y = pos2Sd, yend = pos2Sd), colour = "#FFA500", linetype = "dashed") +
    annotate("text", x = textPos , y = pos2Sd, label = "+2SD", color = sd2Color, hjust = 1, size = sdFontSize) +
#3sd
    geom_segment(aes(x = 1, xend = xend, y = pos3Sd, yend = pos3Sd), colour = "red", linetype = "dashed") +
    annotate("text", x = textPos , y = pos3Sd, label = "+3SD", color = sd3Color, hjust = 1, size = sdFontSize) +
#0 line
    geom_segment(aes(x = 1, xend = xend, y = 0, yend = 0), colour = "#BEBEBE", linetype = "solid") +
#point
    geom_point(aes(colour = factor(ClassNote))) +
#lengend
    scale_colour_manual("", values = sampleCols) +
    scale_x_continuous("", labels = as.character(pc1$SampleID), breaks = ids)
p
dev.off()




